/**
 * @description utils api 路由
 * @author malin
 */

const router = require('koa-router')()
const { loginCheck } = require('../../middlewares/loginChecks')
const koaForm = require('formidable-upload-koa')
const { saveFile } = require('../../controller/utils')

router.prefix('/api/utils')

// 上传图片
router.post('/upload', loginCheck, koaForm(), async (ctx, next) => {
    // 这里files['file']和自定义的ajax有关
    const file = ctx.req.files['file']
    if(!file) return
    const { size, path, name, type } = file
    // 调用controller，移动文件的位置
    ctx.body = await saveFile({
        size,
        name,
        type,
        filePath: path
    })
})

module.exports = router
